package com.qujia.fleet.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.common.utils.StringUtils;
import com.qujia.customer.domain.CustomerCardUseRecord;
import com.qujia.customer.domain.ElectronicCardRefundRecord;
import com.qujia.customer.enums.CardRecordType;
import com.qujia.customer.mapper.CustomerCardUseRecordMapper;
import com.qujia.customer.mapper.ElectronicCardRefundRecordMapper;
import com.qujia.fleet.domain.dto.AppletRechargeRecordDto;
import com.qujia.fleet.domain.dto.FleetCardRechargeRecordDto;
import com.qujia.fleet.domain.vo.AppletRechargeRecordVo;
import com.qujia.fleet.domain.vo.FleetCardRecordBalanceVo;
import com.qujia.fleet.enums.CardRechargeRecordEnum;
import com.qujia.fleet.enums.RechargeTypeEnum;
import com.qujia.fleet.mapper.FleetCardRechargeRecordMapper;
import com.qujia.fleet.service.AppletRechargeRecordService;
import com.qujia.station.domain.Enterprise;
import com.qujia.station.domain.Station;
import com.qujia.station.domain.dto.PhysicalCardBalanceListDto;
import com.qujia.station.enums.BalanceTypeEnum;
import com.qujia.station.enums.PhysicalCardPaymentEnum;
import com.qujia.station.mapper.PhysicalCardBalanceMapper;
import com.qujia.station.service.IEnterpriseService;
import com.qujia.station.service.IStationService;
import com.qujia.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service("appletRechargeRecordService")
@Slf4j
public class AppletRechargeRecordServiceImpl implements AppletRechargeRecordService {

    @Autowired
    private CustomerCardUseRecordMapper customerCardUseRecordMapper;

    @Autowired
    private PhysicalCardBalanceMapper physicalCardBalanceMapper;

    @Autowired
    private FleetCardRechargeRecordMapper fleetCardRechargeRecordMapper;

    @Autowired
    private ElectronicCardRefundRecordMapper electronicCardRefundRecordMapper;

    @Autowired
    private IStationService stationService;

    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private ISysUserService userService;

    @Override
    public List<AppletRechargeRecordVo> selectAppletRechargeRecord(AppletRechargeRecordDto appletRechargeRecordDto) {

        List<AppletRechargeRecordVo> customerCardUseRecordList = new ArrayList<>();
        List<AppletRechargeRecordVo> physicalCardBalanceList = new ArrayList<>();
        List<AppletRechargeRecordVo> fleetCardRechargeRecordList = new ArrayList<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        SysUser sysUser = userService.selectUserById(user.getUserId());
        if (StringUtils.equals("1",sysUser.getEmployeeOrNot())){
            appletRechargeRecordDto.setOperator(loginUser.getUser().getUserId());
            // 统计员工下充值订单 目前只有扫员工码才会记录为该员工下订单
            // 组装数据查询电子卡充值订单
            customerCardUseRecordList = customerCardUseRecord(appletRechargeRecordDto);

            // 组装数据查询实体卡充值订单
            physicalCardBalanceList = physicalCardBalance(appletRechargeRecordDto);
        } else {
            // 组装数据查询电子卡充值订单
            customerCardUseRecordList = customerCardUseRecord(appletRechargeRecordDto);

            // 组装数据查询实体卡充值订单
            physicalCardBalanceList = physicalCardBalance(appletRechargeRecordDto);

            // 组装数据查询车队卡充值订单
            fleetCardRechargeRecordList = fleetCardRechargeRecord(appletRechargeRecordDto);
        }

        if(CollectionUtils.isEmpty(customerCardUseRecordList) && CollectionUtils.isEmpty(physicalCardBalanceList)
                && CollectionUtils.isEmpty(fleetCardRechargeRecordList)){
            return null;
        }

        if(CollectionUtils.isEmpty(physicalCardBalanceList) && CollectionUtils.isEmpty(fleetCardRechargeRecordList)){
            return customerCardUseRecordList;

        }
        if(CollectionUtils.isEmpty(customerCardUseRecordList) && CollectionUtils.isEmpty(fleetCardRechargeRecordList)){
            return physicalCardBalanceList;
        }

        if(CollectionUtils.isEmpty(customerCardUseRecordList) && CollectionUtils.isEmpty(physicalCardBalanceList)){
            return fleetCardRechargeRecordList;
        }

        if(CollectionUtils.isEmpty(fleetCardRechargeRecordList)){
            customerCardUseRecordList.addAll(physicalCardBalanceList);
            return reverse(customerCardUseRecordList);
        }

        if(CollectionUtils.isEmpty(physicalCardBalanceList)){
            customerCardUseRecordList.addAll(fleetCardRechargeRecordList);
            return reverse(customerCardUseRecordList);
        }

        if(CollectionUtils.isEmpty(customerCardUseRecordList)){
            physicalCardBalanceList.addAll(fleetCardRechargeRecordList);
            return reverse(physicalCardBalanceList);
        }

        physicalCardBalanceList.addAll(fleetCardRechargeRecordList);
        customerCardUseRecordList.addAll(physicalCardBalanceList);
        return reverse(customerCardUseRecordList);
    }

    // 排序
    private List<AppletRechargeRecordVo> reverse(List<AppletRechargeRecordVo> result){
        List<AppletRechargeRecordVo> collect = result.stream().sorted(Comparator.comparing(AppletRechargeRecordVo::getCreatedTime)).collect(Collectors.toList());
        CollectionUtil.reverse(collect);
        return collect;
    }

    // 组装数据查询车队卡充值订单
    private List<AppletRechargeRecordVo> fleetCardRechargeRecord(AppletRechargeRecordDto appletRechargeRecordDto){
        if(Objects.nonNull(appletRechargeRecordDto) && RechargeTypeEnum.REFUND.getValue().equals(appletRechargeRecordDto.getOrderType())){
            // 车队卡没有充值退款
            return null;
        }
        FleetCardRechargeRecordDto fleetCardRechargeRecordDto = new FleetCardRechargeRecordDto();
        List<String> cardTypeList = new ArrayList<>();
        cardTypeList.add(CardRechargeRecordEnum.FLEET_RECHARGE.getValue());
        cardTypeList.add(CardRechargeRecordEnum.MANUAL_ADD.getValue());
        cardTypeList.add(CardRechargeRecordEnum.MANUAL_SUB.getValue());

        fleetCardRechargeRecordDto.setCardTypeList(cardTypeList);
        if(Objects.nonNull(appletRechargeRecordDto)){
            fleetCardRechargeRecordDto.setEnterpriseId(appletRechargeRecordDto.getEnterpriseId());
            fleetCardRechargeRecordDto.setStationId(appletRechargeRecordDto.getStationId());
            fleetCardRechargeRecordDto.setFleetOrderNo(appletRechargeRecordDto.getOrderNo());
            fleetCardRechargeRecordDto.setUserName(appletRechargeRecordDto.getUserName());
            fleetCardRechargeRecordDto.setUserPhone(appletRechargeRecordDto.getUserPhone());
            fleetCardRechargeRecordDto.setBeginTime(appletRechargeRecordDto.getBeginTime());
            fleetCardRechargeRecordDto.setEndTime(appletRechargeRecordDto.getEndTime());
            fleetCardRechargeRecordDto.setEnterpriseIdListForAuthor(appletRechargeRecordDto.getEnterpriseIdListForAuthor());
        }

        log.info("fleetCardRechargeRecord 查询车队卡充值记录请求参数：[{}]", JSONObject.toJSONString(fleetCardRechargeRecordDto));
        List<FleetCardRecordBalanceVo> fleetCardRecordList = fleetCardRechargeRecordMapper.selectAppletFleetCardRecordBalance(fleetCardRechargeRecordDto);

        if(CollectionUtils.isEmpty(fleetCardRecordList)){
            log.info("selectAppletRechargeRecord查询车队卡充值订单:[{}]",0);
            return null;
        }
        List<AppletRechargeRecordVo> appletRechargeRecordVoList = new ArrayList<>();
        for (FleetCardRecordBalanceVo fleetCard : fleetCardRecordList) {
            AppletRechargeRecordVo applet = new AppletRechargeRecordVo();
            applet.setOrderNo(fleetCard.getFleetOrderNo());
            applet.setAmt(fleetCard.getRechargeAmount());
            applet.setBalance(fleetCard.getAfterBalance());
            applet.setStationId(fleetCard.getStationId());
            applet.setStationName(fleetCard.getStationName());

            applet.setEnterpriseId(fleetCard.getEnterpriseId());
            applet.setEnterpriseName(fleetCard.getEnterpriseName());

            applet.setPayType(fleetCard.getCardType());
            applet.setStatus("1");//0，未成功；1，成功  车队卡没有状态，默认成功
            applet.setGiftAmount(fleetCard.getGiftAmount());
            applet.setCreatedTime(fleetCard.getCreatedTime());
            applet.setPayTime(fleetCard.getCreatedTime());
            applet.setRechargeCardType("-");
            if(CardRechargeRecordEnum.MANUAL_SUB.getValue().equals(fleetCard.getCardType())) {
                applet.setPayTypeState("-");
            } else {
                applet.setPayTypeState("+");
            }
            applet.setUserName(fleetCard.getUserName());
            applet.setUserPhone(fleetCard.getUserPhone());
            applet.setOilPersonName("");
            appletRechargeRecordVoList.add(applet);
        }
        log.info("selectAppletRechargeRecord查询车队卡充值订单:[{}]",appletRechargeRecordVoList.size());
        return appletRechargeRecordVoList;
    }

    // 组装数据查询实体卡充值订单
    private List<AppletRechargeRecordVo> physicalCardBalance(AppletRechargeRecordDto appletRechargeRecordDto){
        PhysicalCardBalanceListDto physicalCardBalanceListDto = new PhysicalCardBalanceListDto();
        List<String> paymentList = new ArrayList<>();
        paymentList.add(PhysicalCardPaymentEnum.WX_RECHARGE.getValue());
        paymentList.add(PhysicalCardPaymentEnum.POS.getValue());
        paymentList.add(PhysicalCardPaymentEnum.XJ.getValue());
        paymentList.add(PhysicalCardPaymentEnum.MANUAL_ADD.getValue());
        paymentList.add(PhysicalCardPaymentEnum.REFUND.getValue());

        if(Objects.nonNull(appletRechargeRecordDto) && RechargeTypeEnum.RECHARGE.getValue().equals(appletRechargeRecordDto.getOrderType())){
            paymentList.remove(PhysicalCardPaymentEnum.REFUND.getValue());
        } else if(Objects.nonNull(appletRechargeRecordDto) && RechargeTypeEnum.REFUND.getValue().equals(appletRechargeRecordDto.getOrderType())){
            paymentList.remove(PhysicalCardPaymentEnum.WX_RECHARGE.getValue());
            paymentList.remove(PhysicalCardPaymentEnum.POS.getValue());
            paymentList.remove(PhysicalCardPaymentEnum.XJ.getValue());
            paymentList.remove(PhysicalCardPaymentEnum.MANUAL_ADD.getValue());
        }

        physicalCardBalanceListDto.setPaymentList(paymentList);
        if(Objects.nonNull(appletRechargeRecordDto)){
            physicalCardBalanceListDto.setEnterpriseId(appletRechargeRecordDto.getEnterpriseId());
            if(Objects.nonNull(appletRechargeRecordDto.getStationId())) {
                physicalCardBalanceListDto.setStationId(appletRechargeRecordDto.getStationId().toString());
            }
            physicalCardBalanceListDto.setBalanceNo(appletRechargeRecordDto.getOrderNo());
            physicalCardBalanceListDto.setUserName(appletRechargeRecordDto.getUserName());
            physicalCardBalanceListDto.setUserPhone(appletRechargeRecordDto.getUserPhone());
            physicalCardBalanceListDto.setBeginTime(appletRechargeRecordDto.getBeginTime());
            physicalCardBalanceListDto.setEndTime(appletRechargeRecordDto.getEndTime());
            physicalCardBalanceListDto.setEnterpriseIdListForAuthor(appletRechargeRecordDto.getEnterpriseIdListForAuthor());
        }
        physicalCardBalanceListDto.setStatus("1");
        if(Objects.nonNull(appletRechargeRecordDto.getOperator())) {
            physicalCardBalanceListDto.setOperator(appletRechargeRecordDto.getOperator().intValue());
        }
        log.info("physicalCardBalance 查询实体卡充值记录请求参数：[{}]", JSONObject.toJSONString(physicalCardBalanceListDto));
        List<PhysicalCardBalanceListDto> physicalCardBalanceListDtos = physicalCardBalanceMapper.selectPhysicalCardBalanceList(physicalCardBalanceListDto);

        if(CollectionUtils.isEmpty(physicalCardBalanceListDtos)){
            log.info("selectAppletRechargeRecord查询实体卡充值订单:[{}]",0);
            return null;
        }
        List<AppletRechargeRecordVo> appletRechargeRecordVoList = new ArrayList<>();
        for (PhysicalCardBalanceListDto physicalCardBalance : physicalCardBalanceListDtos) {
            AppletRechargeRecordVo applet = new AppletRechargeRecordVo();
            applet.setOrderNo(physicalCardBalance.getBalanceNo());
            applet.setAmt(physicalCardBalance.getAmount());
            applet.setBalance(physicalCardBalance.getBalanceAmount());
            applet.setStationId(Long.valueOf(physicalCardBalance.getStationId()));
            applet.setStationName(physicalCardBalance.getStationName());

            applet.setEnterpriseId(physicalCardBalance.getEnterpriseId());
            applet.setEnterpriseName(physicalCardBalance.getEnterpriseName());

            if(BalanceTypeEnum.RECHARGE.getValue().equals(physicalCardBalance.getBalanceType()) && PhysicalCardPaymentEnum.MANUAL_ADD.getValue().equals(physicalCardBalance.getPayment())) {
                applet.setPayType("czk" + physicalCardBalance.getPayment()); //区分定额卡和储值卡
            } else if(BalanceTypeEnum.RATION_RECHARGE.getValue().equals(physicalCardBalance.getBalanceType()) && PhysicalCardPaymentEnum.MANUAL_ADD.getValue().equals(physicalCardBalance.getPayment())) {
                applet.setPayType("dek" + physicalCardBalance.getPayment());
            }else {
                applet.setPayType(physicalCardBalance.getPayment());
            }
            applet.setStatus(physicalCardBalance.getStatus());//0，未成功；1，成功
            applet.setGiftAmount(BigDecimal.ZERO);
            applet.setCreatedTime(physicalCardBalance.getCreateTime());
            applet.setPayTime(physicalCardBalance.getCreateTime());
            applet.setRechargeCardType("实体卡");
            if(PhysicalCardPaymentEnum.REFUND.getValue().equals(physicalCardBalance.getPayment())) {
                applet.setPayTypeState("-");
            } else {
                applet.setPayTypeState("+");
            }
            applet.setUserName(physicalCardBalance.getUserName());
            applet.setUserPhone(physicalCardBalance.getUserPhone());
            if(Objects.nonNull(physicalCardBalance.getOperator())){
                SysUser sysUser = userService.getById(physicalCardBalance.getOperator().longValue());
                applet.setOilPersonName(sysUser.getNickName());
            }
            appletRechargeRecordVoList.add(applet);
        }
        log.info("selectAppletRechargeRecord查询实体卡充值订单:[{}]",appletRechargeRecordVoList.size());
        return appletRechargeRecordVoList;
    }


    // 组装数据查询电子卡充值订单
    private List<AppletRechargeRecordVo> customerCardUseRecord(AppletRechargeRecordDto appletRechargeRecordDto){
        CustomerCardUseRecord customerCardUseRecord = new CustomerCardUseRecord();
        List<String> stationIdList = new ArrayList<>();
        if(Objects.nonNull(appletRechargeRecordDto) && Objects.nonNull(appletRechargeRecordDto.getEnterpriseId())){
            List<Station> stationList = stationService.getStationByEnterpriseId(appletRechargeRecordDto.getEnterpriseId());
            for (Station station : stationList) {
                if(!stationIdList.contains(station.getStationId().toString())){
                    stationIdList.add(station.getStationId().toString());
                }
            }
        }

        if(Objects.nonNull(appletRechargeRecordDto) && Objects.nonNull(appletRechargeRecordDto.getStationId())
                && !stationIdList.contains(appletRechargeRecordDto.getStationId().toString())){
            stationIdList.add(appletRechargeRecordDto.getStationId().toString());
        }
        List<String> payTypeList = new ArrayList<>();
        payTypeList.add(CardRecordType.WX_RECHARGE.getKey());
        payTypeList.add(CardRecordType.POS_RECHARGE.getKey());
        payTypeList.add(CardRecordType.MANUAL_ADD.getKey());
        payTypeList.add(CardRecordType.MANUAL_SUB.getKey());
        payTypeList.add(CardRecordType.REFUND.getKey());
        if(Objects.nonNull(appletRechargeRecordDto) && RechargeTypeEnum.RECHARGE.getValue().equals(appletRechargeRecordDto.getOrderType())){
            payTypeList.remove(CardRecordType.REFUND.getKey());
        } else if(Objects.nonNull(appletRechargeRecordDto) && RechargeTypeEnum.REFUND.getValue().equals(appletRechargeRecordDto.getOrderType())){
            payTypeList.remove(CardRecordType.WX_RECHARGE.getKey());
            payTypeList.remove(CardRecordType.POS_RECHARGE.getKey());
            payTypeList.remove(CardRecordType.MANUAL_ADD.getKey());
            payTypeList.remove(CardRecordType.MANUAL_SUB.getKey());
        }

        customerCardUseRecord.setStationIdList(stationIdList);
        customerCardUseRecord.setPayTypeList(payTypeList);
        if(Objects.nonNull(appletRechargeRecordDto)){
            customerCardUseRecord.setOrderNo(appletRechargeRecordDto.getOrderNo());
            customerCardUseRecord.setCustomerName(appletRechargeRecordDto.getUserName());
            customerCardUseRecord.setMobilePhone(appletRechargeRecordDto.getUserPhone());
            customerCardUseRecord.setBeginTime(appletRechargeRecordDto.getBeginTime());
            customerCardUseRecord.setEndTime(appletRechargeRecordDto.getEndTime());
            customerCardUseRecord.setEnterpriseIdListForAuthor(appletRechargeRecordDto.getEnterpriseIdListForAuthor());
        }
        customerCardUseRecord.setStatus("1");
        if(Objects.nonNull(appletRechargeRecordDto.getOperator())) {
            customerCardUseRecord.setOperator(appletRechargeRecordDto.getOperator().intValue());
        }
        log.info("customerCardUseRecord 查询电子卡充值记录请求参数：[{}]", JSONObject.toJSONString(customerCardUseRecord));
        List<CustomerCardUseRecord> customerCardUseRecords = customerCardUseRecordMapper.selectCustomerCardUseRecordList(customerCardUseRecord);

        List<AppletRechargeRecordVo> appletRechargeRecordVoList = new ArrayList<>();
        if(CollectionUtils.isEmpty(customerCardUseRecords)){
            log.info("selectAppletRechargeRecord查询电子卡充值订单:[{}]",0);
            return appletRechargeRecordVoList;
        }

        for (CustomerCardUseRecord cardUseRecord : customerCardUseRecords) {
            AppletRechargeRecordVo applet = new AppletRechargeRecordVo();
            applet.setOrderNo(cardUseRecord.getOrderNo());
            applet.setAmt(cardUseRecord.getAmt());
            applet.setBalance(cardUseRecord.getBalance());
            applet.setStationId(cardUseRecord.getStationId().longValue());
            applet.setStationName(cardUseRecord.getStationName());
            Enterprise enterprise = enterpriseService.selectEnterpriseByStationId(cardUseRecord.getStationId().longValue());
            if(Objects.nonNull(enterprise)) {
                applet.setEnterpriseId(enterprise.getEnterpriseId());
                applet.setEnterpriseName(enterprise.getEnterpriseName());
            }
            applet.setPayType(cardUseRecord.getPayType());
            applet.setStatus(cardUseRecord.getStatus());//0，未成功；1，成功
            applet.setGiftAmount(cardUseRecord.getPresentAmt());
            applet.setCreatedTime(cardUseRecord.getCreateTime());
            applet.setPayTime(cardUseRecord.getCreateTime());
            applet.setRechargeCardType("电子卡");
            if(CardRecordType.REFUND.getKey().equals(cardUseRecord.getPayType())) {
                applet.setPayTypeState("-");
                ElectronicCardRefundRecord electronicCardRefundRecord = electronicCardRefundRecordMapper.selectElectronicCardByOrderNo(cardUseRecord.getOrderNo());
                if(Objects.nonNull(electronicCardRefundRecord)){
                    applet.setOriginOrderNo(electronicCardRefundRecord.getOriginOrderNo());
                    CustomerCardUseRecord customerCard = customerCardUseRecordMapper.selectCustomerCardUseRecordByOrderNo(electronicCardRefundRecord.getOriginOrderNo());
                    applet.setOriginPayTime(customerCard.getCreateTime());
                }
            } else if(CardRecordType.MANUAL_SUB.getKey().equals(cardUseRecord.getPayType())){
                applet.setPayTypeState("-");
            } else {
                applet.setPayTypeState("+");
            }
            applet.setUserName(cardUseRecord.getCustomerName());
            applet.setUserPhone(cardUseRecord.getMobilePhone());
            if(Objects.nonNull(cardUseRecord.getOperator())){
                SysUser sysUser = userService.getById(cardUseRecord.getOperator().longValue());
                applet.setOilPersonName(Objects.nonNull(sysUser) ? sysUser.getNickName() : null);
            }


            appletRechargeRecordVoList.add(applet);
        }
        log.info("selectAppletRechargeRecord查询电子卡充值订单:[{}]",appletRechargeRecordVoList.size());
        return appletRechargeRecordVoList;
    }

    @Override
    public AppletRechargeRecordVo selectAppletRechargeRecordStatistics(AppletRechargeRecordDto appletRechargeRecordDto) {
        AppletRechargeRecordVo customerCardUseRecord = new AppletRechargeRecordVo();
        AppletRechargeRecordVo physicalCardBalance = new AppletRechargeRecordVo();
        AppletRechargeRecordVo fleetCardRechargeRecord = new AppletRechargeRecordVo();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        SysUser sysUser = userService.selectUserById(user.getUserId());
        if (StringUtils.equals("1",sysUser.getEmployeeOrNot())){
            appletRechargeRecordDto.setOperator(loginUser.getUser().getUserId());
            // 统计员工下充值订单 目前只有扫员工码才会记录为该员工下订单
            // 组装数据查询电子卡充值订单
            customerCardUseRecord = customerCardUseRecordStatistics(appletRechargeRecordDto);

            // 组装数据查询实体卡充值订单
            physicalCardBalance = physicalCardBalanceStatistics(appletRechargeRecordDto);
        } else {
            // 组装数据查询电子卡充值订单
            customerCardUseRecord = customerCardUseRecordStatistics(appletRechargeRecordDto);

            // 组装数据查询实体卡充值订单
            physicalCardBalance = physicalCardBalanceStatistics(appletRechargeRecordDto);

            // 组装数据查询车队卡充值订单
            fleetCardRechargeRecord = fleetCardRechargeRecordStatistics(appletRechargeRecordDto);
        }

        AppletRechargeRecordVo appletRechargeRecordVo = new AppletRechargeRecordVo();
        appletRechargeRecordVo.setTotalAmt((Objects.nonNull(customerCardUseRecord.getTotalAmt()) ? customerCardUseRecord.getTotalAmt() : BigDecimal.ZERO)
                .add(Objects.nonNull(physicalCardBalance.getTotalAmt()) ? physicalCardBalance.getTotalAmt() : BigDecimal.ZERO)
                .add(Objects.nonNull(fleetCardRechargeRecord.getTotalAmt()) ? fleetCardRechargeRecord.getTotalAmt() : BigDecimal.ZERO));
        appletRechargeRecordVo.setRefundAmt((Objects.nonNull(customerCardUseRecord.getRefundAmt()) ? customerCardUseRecord.getRefundAmt() : BigDecimal.ZERO)
                .add(Objects.nonNull(physicalCardBalance.getRefundAmt()) ? physicalCardBalance.getRefundAmt() : BigDecimal.ZERO)
                .add(Objects.nonNull(fleetCardRechargeRecord.getRefundAmt()) ? fleetCardRechargeRecord.getRefundAmt() : BigDecimal.ZERO));
        appletRechargeRecordVo.setCount((Objects.nonNull(customerCardUseRecord.getCount()) ? customerCardUseRecord.getCount() : 0)
                + (Objects.nonNull(physicalCardBalance.getCount()) ? physicalCardBalance.getCount() : 0) + (Objects.nonNull(fleetCardRechargeRecord.getCount()) ? fleetCardRechargeRecord.getCount() : 0));

        return appletRechargeRecordVo;
    }

    // 组装数据查询车队卡充值订单
    private AppletRechargeRecordVo fleetCardRechargeRecordStatistics(AppletRechargeRecordDto appletRechargeRecordDto){
        BigDecimal totalAmt = BigDecimal.ZERO;
        BigDecimal refundAmt = BigDecimal.ZERO;
        Integer count = 0;
        AppletRechargeRecordVo appletRechargeRecordVo = new AppletRechargeRecordVo();
        if(Objects.nonNull(appletRechargeRecordDto) && RechargeTypeEnum.REFUND.getValue().equals(appletRechargeRecordDto.getOrderType())){
            // 车队卡没有充值退款
            appletRechargeRecordVo.setTotalAmt(totalAmt);
            appletRechargeRecordVo.setRefundAmt(refundAmt);
            appletRechargeRecordVo.setCount(count);
            return appletRechargeRecordVo;
        }
        FleetCardRechargeRecordDto fleetCardRechargeRecordDto = new FleetCardRechargeRecordDto();
        List<String> cardTypeList = new ArrayList<>();
        cardTypeList.add(CardRechargeRecordEnum.FLEET_RECHARGE.getValue());
        cardTypeList.add(CardRechargeRecordEnum.MANUAL_ADD.getValue());
        cardTypeList.add(CardRechargeRecordEnum.MANUAL_SUB.getValue());

        fleetCardRechargeRecordDto.setCardTypeList(cardTypeList);
        if(Objects.nonNull(appletRechargeRecordDto)){
            fleetCardRechargeRecordDto.setEnterpriseId(appletRechargeRecordDto.getEnterpriseId());
            fleetCardRechargeRecordDto.setStationId(appletRechargeRecordDto.getStationId());
            fleetCardRechargeRecordDto.setFleetOrderNo(appletRechargeRecordDto.getOrderNo());
            fleetCardRechargeRecordDto.setUserName(appletRechargeRecordDto.getUserName());
            fleetCardRechargeRecordDto.setUserPhone(appletRechargeRecordDto.getUserPhone());
            fleetCardRechargeRecordDto.setBeginTime(appletRechargeRecordDto.getBeginTime());
            fleetCardRechargeRecordDto.setEndTime(appletRechargeRecordDto.getEndTime());
            fleetCardRechargeRecordDto.setEnterpriseIdListForAuthor(appletRechargeRecordDto.getEnterpriseIdListForAuthor());
        }

        log.info("fleetCardRechargeRecord 查询车队卡充值记录统计请求参数：[{}]", JSONObject.toJSONString(fleetCardRechargeRecordDto));
        List<FleetCardRecordBalanceVo> fleetCardRecordList = fleetCardRechargeRecordMapper.selectAppletFleetCardRecordBalance(fleetCardRechargeRecordDto);


        if(CollectionUtils.isEmpty(fleetCardRecordList)){
            log.info("selectAppletRechargeRecord查询车队卡充值订单统计:[{}]",0);
            appletRechargeRecordVo.setTotalAmt(totalAmt);
            appletRechargeRecordVo.setRefundAmt(refundAmt);
            appletRechargeRecordVo.setCount(count);
            return appletRechargeRecordVo;
        }

        for (FleetCardRecordBalanceVo fleetCard : fleetCardRecordList) {
            if(Objects.nonNull(fleetCard) && !CardRechargeRecordEnum.MANUAL_SUB.getValue().equals(fleetCard.getCardType())){
                totalAmt = totalAmt.add(Objects.nonNull(fleetCard.getRechargeAmount()) ? fleetCard.getRechargeAmount() : BigDecimal.ZERO);
            }
            if(CardRechargeRecordEnum.MANUAL_SUB.getValue().equals(fleetCard.getCardType())){
                totalAmt = totalAmt.subtract(Objects.nonNull(fleetCard.getRechargeAmount()) ? fleetCard.getRechargeAmount() : BigDecimal.ZERO);
            }
        }
        appletRechargeRecordVo.setTotalAmt(totalAmt);
        appletRechargeRecordVo.setRefundAmt(refundAmt);
        appletRechargeRecordVo.setCount(fleetCardRecordList.size());
        log.info("selectAppletRechargeRecord查询车队卡充值订单统计:[{}]",appletRechargeRecordVo);
        return appletRechargeRecordVo;
    }

    // 组装数据查询实体卡充值订单
    private AppletRechargeRecordVo physicalCardBalanceStatistics(AppletRechargeRecordDto appletRechargeRecordDto){
        PhysicalCardBalanceListDto physicalCardBalanceListDto = new PhysicalCardBalanceListDto();
        List<String> paymentList = new ArrayList<>();
        paymentList.add(PhysicalCardPaymentEnum.WX_RECHARGE.getValue());
        paymentList.add(PhysicalCardPaymentEnum.POS.getValue());
        paymentList.add(PhysicalCardPaymentEnum.XJ.getValue());
        paymentList.add(PhysicalCardPaymentEnum.MANUAL_ADD.getValue());
        paymentList.add(PhysicalCardPaymentEnum.REFUND.getValue());

        if(Objects.nonNull(appletRechargeRecordDto) && RechargeTypeEnum.RECHARGE.getValue().equals(appletRechargeRecordDto.getOrderType())){
            paymentList.remove(PhysicalCardPaymentEnum.REFUND.getValue());
        } else if(Objects.nonNull(appletRechargeRecordDto) && RechargeTypeEnum.REFUND.getValue().equals(appletRechargeRecordDto.getOrderType())){
            paymentList.remove(PhysicalCardPaymentEnum.WX_RECHARGE.getValue());
            paymentList.remove(PhysicalCardPaymentEnum.POS.getValue());
            paymentList.remove(PhysicalCardPaymentEnum.XJ.getValue());
            paymentList.remove(PhysicalCardPaymentEnum.MANUAL_ADD.getValue());
        }

        physicalCardBalanceListDto.setPaymentList(paymentList);
        if(Objects.nonNull(appletRechargeRecordDto)){
            physicalCardBalanceListDto.setEnterpriseId(appletRechargeRecordDto.getEnterpriseId());
            if(Objects.nonNull(appletRechargeRecordDto.getStationId())) {
                physicalCardBalanceListDto.setStationId(appletRechargeRecordDto.getStationId().toString());
            }
            physicalCardBalanceListDto.setBalanceNo(appletRechargeRecordDto.getOrderNo());
            physicalCardBalanceListDto.setUserName(appletRechargeRecordDto.getUserName());
            physicalCardBalanceListDto.setUserPhone(appletRechargeRecordDto.getUserPhone());
            physicalCardBalanceListDto.setBeginTime(appletRechargeRecordDto.getBeginTime());
            physicalCardBalanceListDto.setEndTime(appletRechargeRecordDto.getEndTime());
            physicalCardBalanceListDto.setEnterpriseIdListForAuthor(appletRechargeRecordDto.getEnterpriseIdListForAuthor());
        }
        physicalCardBalanceListDto.setStatus("1");
        if(Objects.nonNull(appletRechargeRecordDto.getOperator())) {
            physicalCardBalanceListDto.setOperator(appletRechargeRecordDto.getOperator().intValue());
        }
        log.info("physicalCardBalance 查询实体卡充值记录统计请求参数：[{}]", JSONObject.toJSONString(physicalCardBalanceListDto));
        List<PhysicalCardBalanceListDto> physicalCardBalanceListDtos = physicalCardBalanceMapper.selectPhysicalCardBalanceList(physicalCardBalanceListDto);

        BigDecimal totalAmt = BigDecimal.ZERO;
        BigDecimal refundAmt = BigDecimal.ZERO;
        Integer count = 0;
        AppletRechargeRecordVo appletRechargeRecordVo = new AppletRechargeRecordVo();
        if(CollectionUtils.isEmpty(physicalCardBalanceListDtos)){
            log.info("selectAppletRechargeRecord查询实体卡充值订单统计:[{}]",0);
            appletRechargeRecordVo.setTotalAmt(totalAmt);
            appletRechargeRecordVo.setRefundAmt(refundAmt);
            appletRechargeRecordVo.setCount(count);
            return appletRechargeRecordVo;
        }

        for (PhysicalCardBalanceListDto physicalCardBalance : physicalCardBalanceListDtos) {
            if(Objects.nonNull(physicalCardBalance) && PhysicalCardPaymentEnum.REFUND.getValue().equals(physicalCardBalance.getPayment())){
                refundAmt = refundAmt.add(Objects.nonNull(physicalCardBalance.getAmount()) ? physicalCardBalance.getAmount() : BigDecimal.ZERO);
            } else {
                totalAmt = totalAmt.add(Objects.nonNull(physicalCardBalance.getAmount()) ? physicalCardBalance.getAmount() : BigDecimal.ZERO);
            }
        }
        appletRechargeRecordVo.setTotalAmt(totalAmt);
        appletRechargeRecordVo.setRefundAmt(refundAmt);
        appletRechargeRecordVo.setCount(physicalCardBalanceListDtos.size());
        log.info("selectAppletRechargeRecord查询实体卡充值订单统计:[{}]",appletRechargeRecordVo);
        return appletRechargeRecordVo;
    }


    // 组装数据查询电子卡充值订单
    private AppletRechargeRecordVo customerCardUseRecordStatistics(AppletRechargeRecordDto appletRechargeRecordDto){
        CustomerCardUseRecord customerCardUseRecord = new CustomerCardUseRecord();
        List<String> stationIdList = new ArrayList<>();
        if(Objects.nonNull(appletRechargeRecordDto) && Objects.nonNull(appletRechargeRecordDto.getEnterpriseId())){
            List<Station> stationList = stationService.getStationByEnterpriseId(appletRechargeRecordDto.getEnterpriseId());
            for (Station station : stationList) {
                if(!stationIdList.contains(station.getStationId().toString())){
                    stationIdList.add(station.getStationId().toString());
                }
            }
        }

        if(Objects.nonNull(appletRechargeRecordDto) && Objects.nonNull(appletRechargeRecordDto.getStationId())
                && !stationIdList.contains(appletRechargeRecordDto.getStationId().toString())){
            stationIdList.add(appletRechargeRecordDto.getStationId().toString());
        }
        List<String> payTypeList = new ArrayList<>();
        payTypeList.add(CardRecordType.WX_RECHARGE.getKey());
        payTypeList.add(CardRecordType.POS_RECHARGE.getKey());
        payTypeList.add(CardRecordType.MANUAL_ADD.getKey());
        payTypeList.add(CardRecordType.MANUAL_SUB.getKey());
        payTypeList.add(CardRecordType.REFUND.getKey());
        if(Objects.nonNull(appletRechargeRecordDto) && RechargeTypeEnum.RECHARGE.getValue().equals(appletRechargeRecordDto.getOrderType())){
            payTypeList.remove(CardRecordType.REFUND.getKey());
        } else if(Objects.nonNull(appletRechargeRecordDto) && RechargeTypeEnum.REFUND.getValue().equals(appletRechargeRecordDto.getOrderType())){
            payTypeList.remove(CardRecordType.WX_RECHARGE.getKey());
            payTypeList.remove(CardRecordType.POS_RECHARGE.getKey());
            payTypeList.remove(CardRecordType.MANUAL_ADD.getKey());
            payTypeList.remove(CardRecordType.MANUAL_SUB.getKey());
        }

        customerCardUseRecord.setStationIdList(stationIdList);
        customerCardUseRecord.setPayTypeList(payTypeList);
        if(Objects.nonNull(appletRechargeRecordDto)){
            customerCardUseRecord.setOrderNo(appletRechargeRecordDto.getOrderNo());
            customerCardUseRecord.setCustomerName(appletRechargeRecordDto.getUserName());
            customerCardUseRecord.setMobilePhone(appletRechargeRecordDto.getUserPhone());
            customerCardUseRecord.setBeginTime(appletRechargeRecordDto.getBeginTime());
            customerCardUseRecord.setEndTime(appletRechargeRecordDto.getEndTime());
            customerCardUseRecord.setEnterpriseIdListForAuthor(appletRechargeRecordDto.getEnterpriseIdListForAuthor());
        }
        customerCardUseRecord.setStatus("1");
        if(Objects.nonNull(appletRechargeRecordDto.getOperator())) {
            customerCardUseRecord.setOperator(appletRechargeRecordDto.getOperator().intValue());
        }
        log.info("customerCardUseRecord 查询电子卡充值记录统计请求参数：[{}]", JSONObject.toJSONString(customerCardUseRecord));
        List<CustomerCardUseRecord> customerCardUseRecords = customerCardUseRecordMapper.selectCustomerCardUseRecordList(customerCardUseRecord);

        BigDecimal totalAmt = BigDecimal.ZERO;
        BigDecimal refundAmt = BigDecimal.ZERO;
        Integer count = 0;
        AppletRechargeRecordVo appletRechargeRecordVo = new AppletRechargeRecordVo();
        if(CollectionUtils.isEmpty(customerCardUseRecords)){
            appletRechargeRecordVo.setTotalAmt(totalAmt);
            appletRechargeRecordVo.setRefundAmt(refundAmt);
            appletRechargeRecordVo.setCount(count);
            log.info("selectAppletRechargeRecord查询电子卡充值订单统计:[{}]",0);
            return appletRechargeRecordVo;
        }

        for (CustomerCardUseRecord cardUseRecord : customerCardUseRecords) {
            if(Objects.nonNull(cardUseRecord) && CardRecordType.REFUND.getKey().equals(cardUseRecord.getPayType())
                    && !CardRecordType.MANUAL_SUB.getKey().equals(cardUseRecord.getPayType())){
                refundAmt = refundAmt.add(Objects.nonNull(cardUseRecord.getAmt()) ? cardUseRecord.getAmt() : BigDecimal.ZERO);
            } else {
                totalAmt = totalAmt.add(Objects.nonNull(cardUseRecord.getAmt()) ? cardUseRecord.getAmt() : BigDecimal.ZERO);
            }

            if(CardRecordType.MANUAL_SUB.getKey().equals(cardUseRecord.getPayType())){
                totalAmt = totalAmt.subtract(Objects.nonNull(cardUseRecord.getAmt()) ? cardUseRecord.getAmt() : BigDecimal.ZERO);
            }
        }
        appletRechargeRecordVo.setTotalAmt(totalAmt);
        appletRechargeRecordVo.setRefundAmt(refundAmt);
        appletRechargeRecordVo.setCount(customerCardUseRecords.size());
        log.info("selectAppletRechargeRecord查询电子卡充值订单统计:[{}]",appletRechargeRecordVo);
        return appletRechargeRecordVo;
    }
}
